<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
use app\common\model\FxMoneyLog;

function s($data = null)
{
    $result = [
        'error_code' => 0,
        'result' => $data,
    ];
    return json($result);
}

function e($message, $error_code = 1)
{
    $result = [
        'error_code' => $error_code,
        'message' => $message,
    ];
    $response = \think\Response::create($result, 'json')->header([]);
    throw new \think\exception\HttpResponseException($response);
}
function i($key, $message = "", $type = "string")
{
    if($type=="list"||$type=="array"){
        $key=$key."/a";
    }

    $val = input($key);
    if ($message !== "") {
        if (!isset($val)) {
           e($message);
        }
    }
    if($type==="string"){
        return $val;
    }
    if($type==="int"){
        return intval($val);
    }
    if($type==="float"){
        return floatval($val);
    }
    if($type==="list"){
        if(!is_array($val)){
            $val=[];
        }
        return $val;
    }
    return $val;

}
function getMillisecond()
{
    list($t1, $t2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
function random($len, $type = "number")
{
    $srcstr = "";
    if ($type == "number") {
        $srcstr = "1234567890";
    }
    if ($type == "string") {
        $srcstr = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    }
    mt_srand();
    $strs = "";
    for ($i = 0; $i < $len; $i++) {
        $strs .= $srcstr[mt_rand(0, strlen($srcstr) - 1)];
    }
    return $strs;
}
function now(){
    return date("Y-m-d H:i:s");
}


function c($name, $val = null)
{
    $config = model("config")->where("name", $name)->find();
    if (empty($val)) {
        if (empty($config)) {
            return null;
        }
        return json_decode($config['val'], true);
    }
    if (empty($config)) {
        model("config")->save(['name' => $name, 'val' => json_encode($val)]);
    } else {
        model("config")->save(['val' => json_encode($val)], ['name' => $name]);
    }

}
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}
function doRequest($url, $param = array())
{

    $urlinfo = parse_url($url);

    $host = $urlinfo['host'];
    $path = $urlinfo['path'];
    $query = isset($param) ? http_build_query($param) : '';

    $port = 80;
    $errno = 0;
    $errstr = '';
    $timeout = 30;

    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

    $out = "POST " . $path . " HTTP/1.1\r\n";
    $out .= "host:" . $host . "\r\n";
    $out .= "content-length:" . strlen($query) . "\r\n";
    $out .= "content-type:application/x-www-form-urlencoded\r\n";
    $out .= "connection:close\r\n\r\n";
    $out .= $query;

    fputs($fp, $out);
    fclose($fp);
}
